import { FindOptionsWhere, Like } from 'typeorm';
import { Schedule } from './models/schedule.entity';
import {
  COURSE_CREATE_FAIL,
  COURSE_DEL_FAIL,
  COURSE_NOT_EXIST,
  COURSE_UPDATE_FAIL,
} from './../../common/constants/code';
import { Result } from '@/common/dto/result.type';
import { Args, Mutation, Resolver, Query } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '@/common/guards/auth.guard';
import { SUCCESS } from '@/common/constants/code';
import { ScheduleResult, ScheduleResults } from './dto/result-schedule.output';
import { ScheduleInput } from './dto/schedule.input';
import { ScheduleType } from './dto/schedule.type';
import { ScheduleService } from './schedule.service';
import { CurUserId } from '@/common/decorators/current-user.decorator';
import { PageInput } from '@/common/dto/page.input';
import { CurOrgId } from '@/common/decorators/current-org.decorator';
import dayjs from 'dayjs';

@Resolver(() => ScheduleType)
@UseGuards(GqlAuthGuard)
export class ScheduleResolver {
  constructor(private readonly ScheduleService: ScheduleService) {}

  @Query(() => ScheduleResult)
  async getScheduleInfo(@Args('id') id: string): Promise<ScheduleResult> {
    const result = await this.ScheduleService.findById(id);
    if (result) {
      return {
        code: SUCCESS,
        // data?: result,
        message: '获取成功',
      };
    }
    return {
      code: COURSE_NOT_EXIST,
      message: '课程信息不存在',
    };
  }

  @Mutation(() => ScheduleResult)
  async commitScheduleInfo(
    @Args('params') params: ScheduleInput,
    @CurUserId() userId: string,
    @CurOrgId() orgId: string,
    @Args('id', { nullable: true }) id: string,
  ): Promise<Result> {
    if (!id) {
      const res = await this.ScheduleService.create({
        ...params,
        createdBy: userId,
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '创建成功',
        };
      }
      return {
        code: COURSE_CREATE_FAIL,
        message: '创建失败',
      };
    }
    const Schedule = await this.ScheduleService.findById(userId);
    if (Schedule) {
      const res = await this.ScheduleService.updateById(Schedule.id, {
        ...params,
        updatedBy: userId,
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '更新成功',
        };
      }
      return {
        code: COURSE_UPDATE_FAIL,
        message: '更新失败',
      };
    }
    return {
      code: COURSE_NOT_EXIST,
      message: '课程信息不存在',
    };
  }

  @Query(() => ScheduleResults)
  async getSchedules(
    // @Args('page') page: PageInput,
    // @CurUserId() userId: string,
    @Args('today') today: string,
  ): Promise<ScheduleResults> {
    // const { pageNum, pageSize } = page;
    const where: FindOptionsWhere<Schedule> = {
      schoolDay: dayjs(today).toDate(),
    };
    const [results, total] = await this.ScheduleService.findAllSchedules({
      where,
    });
    return {
      code: SUCCESS,
      // data: results,
      page: {
        total,
      },
      message: '获取成功',
    };
  }

  @Mutation(() => Result)
  async deleteSchedule(
    @Args('id') id: string,
    @CurUserId() userId: string,
  ): Promise<Result> {
    const result = await this.ScheduleService.findById(id);
    if (result) {
      const delRes = await this.ScheduleService.deleteById(id, userId);
      if (delRes) {
        return {
          code: SUCCESS,
          message: '删除成功',
        };
      }
      return {
        code: COURSE_DEL_FAIL,
        message: '删除失败',
      };
    }
    return {
      code: COURSE_NOT_EXIST,
      message: '门店信息不存在',
    };
  }
}
